const express = require("express");
const app = express();
const fs = require("fs");
const path = require('path')
app.listen(3000, function () {
  console.log("3000端口开启了，可以访问: http://192.168.74.213:3000");
});
app.get("/login", (req, res) => {
  let result = JSON.parse(fs.readFileSync("persons.json", "utf8"));
  let a = result.find((item) =>req.query.username == item.username && req.query.password == item.password);
  if (a) {
    res.send("登录成功");
  } else {
    res.send("登录失败");
  }
});
app.get("/register", (req, res) => {
    let result = JSON.parse(fs.readFileSync("persons.json", "utf8"));
    let a = result.find((item) =>req.query.username == item.username);
  if (a) {
    res.send("该账号已注册")
  } else {
    result.push(req.query)
    fs.writeFileSync("persons.json",JSON.stringify (result),{ flag: "w" });
    res.send("注册成功")
  }
});
app.get("/reg",(req, res)=>{
  let filepath = path.join( __dirname,'views/register.html' )
  res.sendFile( filepath )
})
app.get("/log",(req, res)=>{
  let filepath = path.join( __dirname,'views/login.html' )
  res.sendFile( filepath )
})